package org.cashforward.ui.task;

import ca.odell.glazedlists.EventList;
import java.io.Serializable;
import java.util.logging.Logger;
import org.cashforward.ui.UIContext;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;

/**
 * Displays various lists of filters for looking at Payments.
 */
public final class TaskTopComponent extends TopComponent {

    private static TaskTopComponent instance;
    /** path to the icon used by the component and its open action */
//    static final String ICON_PATH = "SET/PATH/TO/ICON/HERE";
    private static final String PREFERRED_ID = "TaskTopComponent";

    private TaskTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(TaskTopComponent.class, "CTL_TaskTopComponent"));
        setToolTipText(NbBundle.getMessage(TaskTopComponent.class, "HINT_TaskTopComponent"));
//        setIcon(Utilities.loadImage(ICON_PATH, true));
        
    }
    
    public PaymentTaskPanel getPaymentTaskPanel(){
        return this.paymentTaskPanel;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        paymentTaskPanel = new org.cashforward.ui.task.PaymentTaskPanel();

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(paymentTaskPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, paymentTaskPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.cashforward.ui.task.PaymentTaskPanel paymentTaskPanel;
    // End of variables declaration//GEN-END:variables
    /**
     * Gets default instance. Do not use directly: reserved for *.settings files only,
     * i.e. deserialization routines; otherwise you could get a non-deserialized instance.
     * To obtain the singleton instance, use {@link findInstance}.
     */
    public static synchronized TaskTopComponent getDefault() {
        if (instance == null) {
            instance = new TaskTopComponent();
        }
        return instance;
    }

    /**
     * Obtain the TaskTopComponent instance. Never call {@link #getDefault} directly!
     */
    public static synchronized TaskTopComponent findInstance() {
        TopComponent win = WindowManager.getDefault().findTopComponent(PREFERRED_ID);
        if (win == null) {
            Logger.getLogger(TaskTopComponent.class.getName()).warning(
                    "Cannot find " + PREFERRED_ID + " component. It will not be located properly in the window system.");
            return getDefault();
        }
        if (win instanceof TaskTopComponent) {
            return (TaskTopComponent) win;
        }
        Logger.getLogger(TaskTopComponent.class.getName()).warning(
                "There seem to be multiple components with the '" + PREFERRED_ID +
                "' ID. That is a potential source of errors and unexpected behavior.");
        return getDefault();
    }

    @Override
    public int getPersistenceType() {
        return TopComponent.PERSISTENCE_ALWAYS;
    }
 
    @Override
    public void componentOpened() {
        EventList scenarios = UIContext.getDefault().getScenarios();
        paymentTaskPanel.setScenarios(scenarios);
        //set the default for now
        getPaymentTaskPanel().getScenarioListComponent().setCheckBoxListSelectedIndex(0);
        getPaymentTaskPanel().getTypeListComponent().setSelectedIndex(2);
    }

    @Override
    public void componentClosed() {
        
    }

    /** replaces this in object stream */
    @Override
    public Object writeReplace() {
        return new ResolvableHelper();
    }

    @Override
    protected String preferredID() {
        return PREFERRED_ID;
    }

    final static class ResolvableHelper implements Serializable {

        private static final long serialVersionUID = 1L;

        public Object readResolve() {
            return TaskTopComponent.getDefault();
        }
    }
}
